package com.dy.drp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dy.drp.dto.RoleDto;
import com.dy.drp.dto.UserDto;
import com.dy.drp.entity.User;
import com.dy.drp.service.IUserService;
import com.dy.drp.util.PageUtil;
import com.dy.drp.util.SelectUtil;
import com.dy.drp.vo.UserAddVo;
import com.dy.drp.vo.UserDtoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dy
 * @since 2022-04-01
 */
@RestController
@RequestMapping("/user")
@Api(tags="用户管理接口")
public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping("/selectOnePageUser/{pageNum}/{pageSize}")
    @ApiOperation("查询单页用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageNum",value="当前页码"),
            @ApiImplicitParam(name="pageSize",value="页面展示条数")
    })
    public PageUtil<UserDtoVo> selectOnePageUser(@PathVariable("pageNum")Integer pageNum,
                                               @PathVariable("pageSize")Integer pageSize){
        PageUtil<UserDto> pageUtil = new PageUtil<>();
        pageUtil.setPageNum(pageNum);
        pageUtil.setPageSize(pageSize);
        PageUtil<UserDto> pageUtil1 = userService.selectOnePageUser(pageUtil);

        PageUtil<UserDtoVo> pageUtilVo = new PageUtil<>();
        pageUtilVo.setPageNum(pageNum);
        pageUtilVo.setPageSize(pageSize);
        pageUtilVo.setCount(pageUtil1.getCount());
        pageUtilVo.setTotalPage(pageUtil1.getCount());
        List<UserDtoVo> list = new ArrayList<>();
        pageUtil1.getList().stream().forEach(r->{
            String power = "";
            for (int i = 0; i < r.getRoleList().size(); i++) {
                if (i < r.getRoleList().size()-1)
                power += r.getRoleList().get(i).getRoleName() + "、";
                else
                power += r.getRoleList().get(i).getRoleName() ;
            }
            UserDtoVo userDtoVo = new UserDtoVo(r.getUserId(),r.getUserName(),r.getUserPwd(),r.getRealName(),power);
            list.add(userDtoVo);
        });
        pageUtilVo.setList(list);

        return pageUtilVo;
    }

    @PostMapping("/selectOnePageUserByInfo")
    @ApiOperation("通过用户名模糊查询单页用户信息")
    public PageUtil<UserDto> selectOnePageUserByInfo(SelectUtil selectUtil){
        return userService.selectOnePageUserByInfo(selectUtil);
    }

    @PostMapping("/insertUser")
    @ApiOperation("添加新用户")
    public String insertUser(UserAddVo userAddVo){
        UserDto userDto = new UserDto();
        userDto.setUserName(userAddVo.getUserName());
        userDto.setRealName(userAddVo.getRealName());
        List<RoleDto> roleList = new ArrayList<>();
        userAddVo.getRoleList().stream().forEach(r->{
            RoleDto roleDto = new RoleDto(r);
            roleList.add(roleDto);
        });
        userDto.setRoleList(roleList);
        if (userService.insertUser(userDto)){
            return "添加成功";
        }else {
            return "添加失败";
        }
    }

    @GetMapping("/deleteUser/{userName}")
    @ApiOperation("删除用户--通过用户名")
    @ApiImplicitParam(name="userName",value = "用户名")
    public String deleteUser(@PathVariable("userName")String userName){
        if (userService.deleteUser(userName)){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }

    @PostMapping("/updateUserRole")
    @ApiOperation("修改用户权限")
    public String updateUserRole(UserAddVo userAddVo){
        UserDto userDto = new UserDto();
        userDto.setUserName(userAddVo.getUserName());
        userDto.setRealName(userAddVo.getRealName());
        List<RoleDto> roleList = new ArrayList<>();
        userAddVo.getRoleList().stream().forEach(r->{
            RoleDto roleDto = new RoleDto(r);
            roleList.add(roleDto);
        });
        userDto.setRoleList(roleList);
        if (userService.updateUserRole(userDto)){
            return "修改成功";
        }else{
            return "修改失败";
        }
    }

    @GetMapping("/selectOneUserByName/{userName}")
    @ApiOperation("通过用户名查询单个用户信息 -- 修改用户使用")
    @ApiImplicitParam(name="userName",value = "用户名")
    public UserDto selectOneUserByName(@PathVariable("userName")String userName){
        return userService.selectOneUserByName(userName);
    }

    @GetMapping("/updateUserPwd/{userName}/{oldPwd}/{newPwd}")
    @ApiOperation("修改用户密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userName",value = "用户名"),
            @ApiImplicitParam(name = "oldPwd",value = "旧密码"),
            @ApiImplicitParam(name="newPwd",value = "新密码")
    })
    public String updateUserPwd(@PathVariable("userName")String userName,
                                @PathVariable("oldPwd")String oldPwd,
                                @PathVariable("newPwd")String newPwd){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",userName);
        wrapper.eq("user_pwd",oldPwd);
        User one = userService.getOne(wrapper);
        if (one == null ){
            return "密码错误";
        }
        one.setUserPwd(newPwd);
        boolean update = userService.update(one, wrapper);
        if (update){
            return "密码修改成功";
        }
        return "密码修改失败";
    }
}
